using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Linq.Expressions;

namespace MoleCart.Core.Common {

	#region 公用类库
	/// <summary>
	/// 公用类库
	/// </summary>
	/// Create by jerry. 2011/6/27 11:59
	public class CommonLib {
		/// <summary>
		/// 分页常量，开始行号
		/// </summary>
		public const string STARTROW = "StartRow";
		/// <summary>
		/// 分页常量，结束行号
		/// </summary>
		public const string ENDROW = "EndRow";
		/// <summary>
		/// 分页常量，每页记录数
		/// </summary>
		public const string PAGESIZE = "PageSize";
		/// <summary>
		/// 分页常量，开始行号-1，开始行号前一行记录
		/// </summary>
		public const string STARTROWPRIOR = "StartRowPrior";

		/// <summary>
		/// 获得分页使用的Hashtable
		/// </summary>
		/// <param name="startRow">The start row.</param>
		/// <param name="endRow">The end row.</param>
		/// <param name="pageSize">Size of the page.</param>
		/// <returns></returns>
		/// Create by jerry. 2011/6/27 12:00
		public static Hashtable GetQueryHashtable(int startRow, int endRow, int pageSize) {
			Hashtable ht = new Hashtable();
			ht[CommonLib.STARTROW] = startRow;
			ht[CommonLib.ENDROW] = endRow;
			ht[CommonLib.PAGESIZE] = pageSize;
			ht[CommonLib.STARTROWPRIOR] = startRow - 1;
			return ht;
		}

		/// <summary>
		/// 将obejct转换为Hashtable
		/// </summary>
		/// <param name="o">The o.</param>
		/// <returns></returns>
		/// Create by jerry. 2011/6/27 12:00
		public static Hashtable Convert2Hashtable(object o) {
			Hashtable ht = new Hashtable();
			if (o == null)
				return ht;
			Type t = o.GetType();
			PropertyInfo[] list = t.GetProperties();
			foreach (PropertyInfo p in list) {
				if (!ht.ContainsKey(p.Name)) {
					string key = p.Name;
					object value = p.GetValue(o, null);
					if (p.PropertyType == typeof(string) && (value != null)) {
						value = value.ToString().Trim();
						//if (value.ToString().Length == 0)
						//{
						//    value = null;
						//}
					}
					else if (p.PropertyType.BaseType == typeof(Enum) && (value != null)) {
						value = Convert.ToInt32(value);
					}
					ht.Add(key, value);
				}
			}
			return ht;
		}

		/// <summary>
		/// 合并2个Hashtable
		/// </summary>
		/// <param name="a">A.</param>
		/// <param name="b">The b.</param>
		/// <returns></returns>
		/// Create by jerry. 2011/6/27 12:00
		public static Hashtable MergeHashtable(Hashtable a, Hashtable b) {
			foreach (DictionaryEntry var in b)
				a.Add(var.Key, var.Value);
			return a;
		}


		/// <summary>
		/// 计算分页
		/// </summary>
		/// <param name="pageIndex">Index of the page.</param>
		/// <param name="pageSize">Size of the page.</param>
		/// <param name="recordCount">The record count.</param>
		/// <param name="startRow">The start row.</param>
		/// <param name="endRow">The end row.</param>
		/// <param name="pageCount">The page count.</param>
		/// Create by jerry. 2011/6/27 12:49
		public static void CPage(int pageIndex, int pageSize, int recordCount, out int startRow, out int endRow, out int pageCount) {
			pageCount = (int)Math.Ceiling(recordCount / (double)pageSize);
			pageIndex = pageIndex > pageCount ? pageCount : pageIndex;
			pageIndex = pageIndex <= 0 ? 1 : pageIndex;
			startRow = (pageIndex - 1) * pageSize + 1;
			endRow = pageIndex * pageSize;
		}

	}

	/// <summary>
	/// 公用类库泛型版本
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// Create by jerry. 2011/6/27 12:51
	public class CommonLib<T> where T : class {
		/// <summary>
		/// 将 T 转换为Hashtable
		/// </summary>
		/// <param name="o">The o.</param>
		/// <returns></returns>
		/// Create by jerry. 2011/6/27 12:00
		public static Hashtable Convert2Hashtable(T o) {
			Hashtable ht = new Hashtable();
			Type t = o.GetType();
			PropertyInfo[] list = t.GetProperties();
			foreach (PropertyInfo p in list) {
				if (!ht.ContainsKey(p.Name)) {
					string key = p.Name;
					object value = p.GetValue(o, null);
					if (p.PropertyType == typeof(string) && (value != null)) {
						value = value.ToString().Trim();
						if (value.ToString().Length == 0) {
							value = null;
						}
					}
					ht.Add(key, value);
				}
			}
			return ht;
		}
	}





	#endregion

	public enum SortType {
		Asc,
		Desc
	}

}
